package com.ophiux.cs.api.consult.impl;

import java.lang.reflect.Type;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.gson.reflect.TypeToken;
import com.ophiux.cs.api.consult.port.ICSChatApiPort;
import com.ophiux.cs.application.chat.dto.GetCSMessagesRequest;
import com.ophiux.cs.application.chat.dto.ReadCSMessageRequest;
import com.ophiux.cs.application.chat.dto.SendCSMessageRequest;
import com.ophiux.cs.application.chat.dto.UndoCSMessageRequest;
import com.ophiux.cs.application.chat.service.IQueryCSMessagesAppService;
import com.ophiux.cs.application.chat.service.IReadCSMessageAppService;
import com.ophiux.cs.application.chat.service.ISendCSMessageAppService;
import com.ophiux.cs.application.chat.service.IUndoCSMessageAppService;
import com.ophiux.cs.application.core.AppResultCode;
import com.ophiux.cs.application.core.Request;
import com.ophiux.cs.application.core.Result;
import com.ophiux.utils.parser.json.JsonUtils;
import com.ophiux.utils.parser.json.JsonUtils.TypeObtainCallback;

import lombok.extern.slf4j.Slf4j;

/**
 * 聊天插件
 * @author chenh
 *
 */
@Service("cSChatApiService")
@Slf4j
public class CSChatApiService implements ICSChatApiPort{

	@Autowired
	IQueryCSMessagesAppService queryCSMessagesAppService;
	
	@Autowired
	IReadCSMessageAppService readCSMessageAppService;
	
	@Autowired
	ISendCSMessageAppService sendCSMessageAppService;
	
	@Autowired
	IUndoCSMessageAppService undoCSMessageAppService;
	
	@Override
	public String SendCSMessage(String data) {
		log.info("SendCSMessage接口,请求参数："+data);
		Result result = null;
		Request<SendCSMessageRequest> request = null;
		try {
			request = JsonUtils.toEntity(data, new TypeObtainCallback() {
				public Type getObtainType() {
					return new TypeToken<Request<SendCSMessageRequest>>() {
					}.getType();
				}
			});
			
			result = sendCSMessageAppService.sendConMessage(request.getPARAM());
			
		} catch (Exception e) {
			log.info("SendCSMessage接口异常：",e);
			e.printStackTrace();
			result = Result.fail(AppResultCode.EXCEPTION, e.getMessage());
			return JsonUtils.toJson(result);
		}
			
		String returnMessage = JsonUtils.toJson(result);
		log.info("SendCSMessage接口,返回参数："+returnMessage);
		return returnMessage;
	}

	@Override
	public String GetCSChatMessages(String data) {
		log.info("GetCSChatMessages接口,请求参数："+data);
		Result result = null;
		Request<GetCSMessagesRequest> request = null;
		try {
			request = JsonUtils.toEntity(data, new TypeObtainCallback() {
				public Type getObtainType() {
					return new TypeToken<Request<GetCSMessagesRequest>>() {
					}.getType();
				}
			});
			
			result = queryCSMessagesAppService.GetCSChatMessages(request.getPARAM());
			
		} catch (Exception e) {
			log.info("GetCSChatMessages接口异常：",e);
			e.printStackTrace();
			result = Result.fail(AppResultCode.EXCEPTION, e.getMessage());
			return JsonUtils.toJson(result);
		}
			
		String returnMessage = JsonUtils.toJson(result);
		log.info("GetCSChatMessages接口,返回参数："+returnMessage);
		return returnMessage;
	}

	@Override
	public String ReadCSMessage(String data) {
		log.info("GetCSChatMessages接口,请求参数："+data);
		Result result = null;
		Request<ReadCSMessageRequest> request = null;
		try {
			request = JsonUtils.toEntity(data, new TypeObtainCallback() {
				public Type getObtainType() {
					return new TypeToken<Request<ReadCSMessageRequest>>() {
					}.getType();
				}
			});
			
			result = readCSMessageAppService.ReadConMessage(request.getPARAM());
			
		} catch (Exception e) {
			log.info("GetCSChatMessages接口异常：",e);
			e.printStackTrace();
			result = Result.fail(AppResultCode.EXCEPTION, e.getMessage());
			return JsonUtils.toJson(result);
		}
			
		String returnMessage = JsonUtils.toJson(result);
		log.info("GetCSChatMessages接口,返回参数："+returnMessage);
		return returnMessage;
	}

	@Override
	public String UndoCSMessage(String data) {
		log.info("UndoCSMessage接口,请求参数："+data);
		Result result = null;
		Request<UndoCSMessageRequest> request = null;
		try {
			request = JsonUtils.toEntity(data, new TypeObtainCallback() {
				public Type getObtainType() {
					return new TypeToken<Request<UndoCSMessageRequest>>() {
					}.getType();
				}
			});
			
			result = undoCSMessageAppService.UndoChatMessage(request.getPARAM());
			
		} catch (Exception e) {
			log.info("UndoCSMessage接口异常：",e);
			e.printStackTrace();
			result = Result.fail(AppResultCode.EXCEPTION, e.getMessage());
			return JsonUtils.toJson(result);
		}
			
		String returnMessage = JsonUtils.toJson(result);
		log.info("UndoCSMessage接口,返回参数："+returnMessage);
		return returnMessage;
	}
	
	
}
